package com.dongdongshop.service.impl;

import com.dongdongshop.mapper.TbBrandMapper;
import com.dongdongshop.model.TbBrand;
import com.dongdongshop.model.TbBrandExample;
import com.dongdongshop.page.PageParms;
import com.dongdongshop.page.PageResult;
import com.dongdongshop.service.BrandService;
import com.dongdongshop.vo.BrandVo;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.List;
import java.util.stream.Collectors;

@Service
public class BrandServiceImpl implements BrandService {

    @Resource
    private TbBrandMapper bm;


    //查询

    @Override
    public List<BrandVo> brandList(BrandVo brandVo) {
        TbBrandExample example = new TbBrandExample();
        TbBrandExample.Criteria criteria = example.createCriteria();
        if (StringUtils.isNoneBlank(brandVo.getFirstChar())){
            criteria.andFirstCharEqualTo(brandVo.getFirstChar());
        }
        if (StringUtils.isNoneBlank(brandVo.getName())){
            criteria.andNameLike("%"+brandVo.getName()+"%");
        }

        List<TbBrand> tbBrands = bm.selectByExample(example);
        return tbBrands.stream().map(brand ->{
            BrandVo vo = new BrandVo();
            BeanUtils.copyProperties(brand,vo);
            return vo;
        }).collect(Collectors.toList());
    }

    //回显

    @Override
    public BrandVo toupdate(Long id) {
        TbBrand tbBrand = bm.selectByPrimaryKey(id);
        BrandVo vo = new BrandVo();
        BeanUtils.copyProperties(tbBrand,vo);
        return vo;
    }

    //修改

    @Override
    public void update(BrandVo vo) {
        TbBrand brand = new TbBrand();
        BeanUtils.copyProperties(vo,brand);
        bm.updateByPrimaryKeySelective(brand);
    }

    //增加

    @Override
    public void add(BrandVo vo) {
        TbBrand brand = new TbBrand();
        BeanUtils.copyProperties(vo,brand);
        bm.insert(brand);
    }

    //删除

    @Override
    public PageResult brandPage(PageParms<BrandVo> page) {
        BrandVo brandVo = page.getParm();
        TbBrandExample example = new TbBrandExample();
        TbBrandExample.Criteria criteria = example.createCriteria();
        if (StringUtils.isNoneBlank(brandVo.getFirstChar())){
            criteria.andFirstCharEqualTo(brandVo.getFirstChar());
        }
        if (StringUtils.isNoneBlank(brandVo.getName())){
            criteria.andNameLike("%"+brandVo.getName()+"%");
        }
        PageHelper.startPage(page.getPageNum(), page.getPageSize());
        List<TbBrand> tbBrands = bm.selectByExample(example);
        PageInfo pageInfo = new PageInfo(tbBrands);
        List<BrandVo> brandVoList = tbBrands.stream().map(brand -> {
            BrandVo vo = new BrandVo();
            BeanUtils.copyProperties(brand, vo);
            return vo;
        }).collect(Collectors.toList());
        return new PageResult(page.getPageNum(),page.getPageSize(),brandVoList,pageInfo.getTotal());
    }

    @Override
    public void delete(Long id) {
        bm.deleteByPrimaryKey(id);
    }
}
